#include "usermodel.h"
#include "mysql.h"
#include <iostream>
#include <vector>
using namespace std;
using namespace ChatRoom;
// User表的增加方法
bool UserModel::insert(User &user)
{
    // 1.组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "insert into user(name, password, state) values('%s', '%s', '%s')",
            user.getName().c_str(), user.getPwd().c_str(), user.getState().c_str());

    MySQL mysql;
    if (mysql.Connect())
    {
        if (mysql.Update(sql))
        {
            // 获取插入成功的用户数据生成的主键id
            user.setId(mysql_insert_id(mysql.GetConnection()));
            return true;
        }
    }

    return false;
}

// 根据用户号码查询用户信息
vector<User> UserModel::query(int id)
{
    // 1.组装sql语句
    char sql[1024] = {0};
    if(id != 0){
        sprintf(sql, "select * from user where id = %d", id);
    }else{
        sprintf(sql, "select * from user ");
    }
    vector<User> vec;
    MySQL mysql;
    if (mysql.Connect())
    {
        MYSQL_RES *res = mysql.Query(sql);
        if (res != nullptr)
        {
            MYSQL_ROW row ;
            while ((row= mysql_fetch_row(res)) != nullptr)
            {
                User user;
                user.setId(atoi(row[0]));
                user.setName(row[1]);
                user.setPwd(row[2]);
                user.setState(row[3]);
                vec.push_back(user);
            }
            mysql_free_result(res);
            return vec;
        }
    }

    return vec;
}

// 根据用户名字查询用户信息
vector<User> UserModel::query(string name)
{
    // 1.组装sql语句
    char sql[1024] = {0};
    if(name != "all"){
        cout<<"name is "<<name.c_str()<<endl;
        sprintf(sql, "select * from user where name = '%s'", name.c_str());
    }else{
        sprintf(sql, "select * from user ");
    }
    vector<User> vec;
    MySQL mysql;
    if (mysql.Connect())
    {
        MYSQL_RES *res = mysql.Query(sql);
        if (res != nullptr)
        {
            MYSQL_ROW row ;
            while ((row= mysql_fetch_row(res)) != nullptr)
            {
                User user;
                user.setId(atoi(row[0]));
                user.setName(row[1]);
                user.setPwd(row[2]);
                user.setState(row[3]);
                vec.push_back(user);
            }
            mysql_free_result(res);
            return vec;
        }
    }

    return vec;
}

// 更新用户的状态信息
bool UserModel::updateState(User user)
{
    // 1.组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "update user set state = '%s' where id = %d", user.getState().c_str(), user.getId());

    MySQL mysql;
    if (mysql.Connect())
    {
        if (mysql.Update(sql))
        {
            return true;
        }
    }
    return false;
}

// 更新用户的姓名信息
bool UserModel::updateName(User user)
{
    // 1.组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "update user set name = '%s' where id = %d", user.getName().c_str(), user.getId());

    MySQL mysql;
    if (mysql.Connect())
    {
        if (mysql.Update(sql))
        {
            return true;
        }
    }
    return false;
}
//删除用户信息
bool UserModel::deleteUser(int id){
    char sql[1024] = {0};
    sprintf(sql, "delete from user where id = %d", id);

    MySQL mysql;
    if (mysql.Connect())
    {
        if (mysql.Delete(sql))
        {
            return true;
        }
    }
    return false;   
}
 // 统计用户数量
 int UserModel::userCount(){
    char sql[1024] = {0};
    int count = -1;
    sprintf(sql, "select count(*) from user");
    MySQL mysql;
    if (mysql.Connect())
    {
        MYSQL_RES *res = mysql.Query(sql);
        if (res != nullptr)
        {
            MYSQL_ROW row ;
            if ((row= mysql_fetch_row(res)) != nullptr)
            {
                count = atoi(row[0]);
            }
            mysql_free_result(res);
        }
    }
    return count;
 }
// 重置用户的状态信息
void UserModel::resetState()
{
    // 1.组装sql语句
    char sql[1024] = "update user set state = 'offline' where state = 'online'";

    MySQL mysql;
    if (mysql.Connect())
    {
        mysql.Update(sql);
    }
}
